<?php
/**
 * Created by PhpStorm.
 * User: sunjian
 * Date: 2018/3/18
 * Time: 20:01
 */

namespace frontend\controllers;


use app\models\Area;
use app\models\User;
use Yii;

class ManagerController extends BaseController
{
    public function actionLogin(){
        $this->layout="login";
        Yii::$app->user->logout();
        return $this->render("login");
    }
    public function actionSign(){
        $request = Yii::$app->request;
        $return = [];
        if($request->isAjax){
            $username = $request->post("username");
            $password = $request->post("password");
            $user = User::findOne(['username'=>$username]);
            if($user){
                if(Yii::$app->security->validatePassword($password,$user->password)){
                    Yii::$app->user->login($user);
                    $return['status']=true;

                }else{
                    $return['status']=false;
                    $return['msg']="用户名或密码错误";

                }
            }else{
                $return['status']=false;
                $return['msg']="用户名或密码错误";


            }
        }
        return json_encode($return);
    }

    public function actionIndex(){
        $areas = Area::find()->where(['flag'=>1])->asArray()->all();

        return $this->render("index",['areas'=>$areas]);
    }

    public function actionInfo(){
        $request = Yii::$app->request;
        $id = $request->get('area');
        if($id==0){
            $areas = Area::find()->where(['flag'=>1])->asArray()->all();
            return $this->render("total",['areas'=>$areas]);
        }else{
            $area = Area::findOne($id);
            return $this->render("area",['area'=>$area]);
        }
    }
}